<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="50%">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="110px"
      v-loading="formLoading"
    >
      <el-form-item label="文章id" prop="articleId">
        <el-input :readonly="formType == 'detail'" v-model="formData.articleId" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input :readonly="formType == 'detail'" v-model="formData.title" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input :readonly="formType == 'detail'" v-model="formData.author" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="摘要" prop="digest">
        <el-input :readonly="formType == 'detail'" v-model="formData.digest" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <Editor :readonly="formType == 'detail'" v-model="formData.content" height="150px" />
      </el-form-item>
      <el-form-item label="原文地址" prop="contentSourceUrl">
        <el-input :readonly="formType == 'detail'" v-model="formData.contentSourceUrl" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="封面图片素材id" prop="thumbMediaId">
        <el-input :readonly="formType == 'detail'" v-model="formData.thumbMediaId" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="是否显示封面" prop="showCoverPic">
        <el-select
          :disabled="formType == 'detail'"
          v-model="formData.showCoverPic"
          clearable
          filterable
          class="!w-240px"
        >
          <el-option key="1" label="显示" value="1" />
          <el-option key="0" label="不显示" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否打开评论" prop="needOpenComment">
        <el-select
          :disabled="formType == 'detail'"
          v-model="formData.needOpenComment"
          clearable
          filterable
          class="!w-240px"
        >
          <el-option key="1" label="打开" value="1" />
          <el-option key="0" label="不打开" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否仅粉丝可评论" prop="onlyFansCanComment">
        <el-select
          :disabled="formType == 'detail'"
          v-model="formData.onlyFansCanComment"
          clearable
          filterable
          class="!w-240px"
        >
          <el-option key="1" label="仅粉丝可评论" value="1" />
          <el-option key="0" label="所有人可评论" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="图文消息URL" prop="url">
        <el-input :readonly="formType == 'detail'" v-model="formData.url" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="该图文是否被删除" prop="isDeleted">
        <el-select
          :disabled="formType == 'detail'"
          v-model="formData.isDeleted"
          clearable
          filterable
          class="!w-240px"
        >
          <el-option key="1" label="是" value="1" />
          <el-option key="0" label="否" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="文章创建时间" prop="articleCreateTime">
        <el-date-picker
          v-model="formData.articleCreateTime"
          :readonly="formType == 'detail'"
          type="date"
          value-format="x"
          placeholder="请选择"
        />
      </el-form-item>
      <el-form-item label="文章最后更新时间" prop="articleUpdateTime">
        <el-date-picker
          v-model="formData.articleUpdateTime"
          :readonly="formType == 'detail'"
          type="date"
          value-format="x"
          placeholder="请选择"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button v-if="formType != 'detail'" @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { WeixinArticleApi, WeixinArticleVO } from '@/api/integration/weixinarticle'

/** 微信文章 表单 */
defineOptions({ name: 'WeixinArticleForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  articleId: undefined,
  title: undefined,
  author: undefined,
  digest: undefined,
  content: undefined,
  contentSourceUrl: undefined,
  thumbMediaId: undefined,
  showCoverPic: undefined,
  needOpenComment: undefined,
  onlyFansCanComment: undefined,
  url: undefined,
  isDeleted: undefined,
  articleCreateTime: undefined,
  articleUpdateTime: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await WeixinArticleApi.getWeixinArticle(id)
      formData.value.showCoverPic = formData.value.showCoverPic + ''
      formData.value.needOpenComment = formData.value.needOpenComment + ''
      formData.value.onlyFansCanComment = formData.value.onlyFansCanComment + ''
      formData.value.isDeleted = formData.value.isDeleted + ''
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as WeixinArticleVO
    if (formType.value === 'create') {
      await WeixinArticleApi.createWeixinArticle(data)
      message.success(t('common.createSuccess'))
    } else {
      await WeixinArticleApi.updateWeixinArticle(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    articleId: undefined,
    title: undefined,
    author: undefined,
    digest: undefined,
    content: undefined,
    contentSourceUrl: undefined,
    thumbMediaId: undefined,
    showCoverPic: undefined,
    needOpenComment: undefined,
    onlyFansCanComment: undefined,
    url: undefined,
    isDeleted: undefined,
    articleCreateTime: undefined,
    articleUpdateTime: undefined
  }
  formRef.value?.resetFields()
}
</script>
